package com.work.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.work.utils.BigDecimalSerializer;
import lombok.Data;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
public class ShiftInfoForAssignWorkVO {

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    private LocalDateTime updateTime;
    private String timeSpace;
    private String timeSpaceName;
    private String shipName;
    private String billNo;
    private String tallyUser;
    private String tallySign;
    private String checker;
    private String checkSign;
    @JsonSerialize(using = BigDecimalSerializer.class, nullsUsing = NullSerializer.class)
    private BigDecimal totalTon;
    @JsonSerialize(using = BigDecimalSerializer.class, nullsUsing = NullSerializer.class)
    private BigDecimal totalPiece;
    @JsonSerialize(using = ToStringSerializer.class)
    private Long arrangeId;

    private List<ShiftInfoForAssignWorkVO> data;

    private String holdName;
    private String cargoTypeName;
    private String packageTypeName;
    @JsonSerialize(using = BigDecimalSerializer.class, nullsUsing = NullSerializer.class)
    private BigDecimal checkTon;
    @JsonSerialize(using = BigDecimalSerializer.class, nullsUsing = NullSerializer.class)
    private BigDecimal checkPiece;
    private String operationTypeName;
    private String workerInfo;
    private String machineInfo;
    private String areaName;
    private String positionName;
    private String spot;
    private String createUser;
    private String createUserName;
    private String hold;
    private String packageType;
    private String operationType;
    private String checkerName;
    private String cargoType;

    public BigDecimal getTotalPiece() {
        if (Func.isNotEmpty(data)) {
            return data.stream().map(ShiftInfoForAssignWorkVO::getCheckPiece).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return totalPiece;
    }

    public BigDecimal getTotalTon() {
        if (Func.isNotEmpty(data)) {
            return data.stream().map(ShiftInfoForAssignWorkVO::getCheckTon).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return totalTon;
    }

    public BigDecimal getCheckTon() {
        if (Func.isNull(checkTon)) {
            return BigDecimal.ZERO;
        }
        return checkTon;
    }

    public BigDecimal getCheckPiece() {
        if (Func.isNull(checkPiece)) {
            return BigDecimal.ZERO;
        }
        return checkPiece;
    }

    public String getAreaName() {
        if (Func.isNotBlank(areaName) && "DIRECT_AREA".equals(areaName)) {
            return "";
        }
        return areaName;
    }
}
